package com.zsc.controller.admin;

import com.zsc.dto.CategoryDTO;
import com.zsc.dto.SetmealDTO;
import com.zsc.dto.SetmealPageQueryDTO;
import com.zsc.entity.Category;
import com.zsc.result.PageResult;
import com.zsc.result.Result;
import com.zsc.service.CategoryService;
import com.zsc.service.SetmealService;
import com.zsc.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Enkidu
 * @Date 2024/6/13 18:55
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "管理端 套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping("/")
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("查询套餐,{}", setmealPageQueryDTO);
        PageResult result = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除套餐 ,ids:{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @ApiOperation("查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> queryById(@PathVariable Long id) {
        log.info("根据id查询套餐 ,id:{}", id);
        SetmealVO setmealVO = setmealService.queryById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐 启/停 售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改套餐状态: {},id为{}", status, id);
        setmealService.changeStatus(status, id);
        return Result.success();
    }

}
